package com.vadmin.vadminjava.servlet;

import com.alibaba.fastjson.JSONObject;
import com.vadmin.vadminjava.service.WxWorkClientService;
import com.vadmin.vadminjava.service.WxWorkCommon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/* ***********************************************************
 *
 * 企业微信的客户端相关接口
 *
 * **********************************************************/
@RestController
@RequestMapping ("/WxWorkClientServlet")
public class WxWorkClientServlet {

	@Autowired
	WxWorkClientService wxWorkClientService;
	@Autowired
	WxWorkCommon wxWorkCommon;


	//企业微信登录（员工和外部客户共用）
	@RequestMapping(value = {"/workerLogin"})
	public String login(@RequestParam Map<String, Object> params,HttpServletRequest httpServletRequest) {
		JSONObject paramJ = new JSONObject();
		params.keySet().forEach(i -> paramJ.put(i, params.get(i)));
		System.out.println("workerLogin被调用了？？？"+httpServletRequest.getRemoteHost());
		return wxWorkClientService.workerLogin(paramJ).toJSONString();
	}

	//获取外部客户的信息
	@RequestMapping(value = {"/externalcontactDetail"})
	public String externalcontactDetail(@RequestParam Map<String, Object> params) {
		JSONObject paramJ = new JSONObject();
		params.keySet().forEach(i -> paramJ.put(i, params.get(i)));
		//黄浮驴：wmJRSsWwAA4c4EdckQvJsNqwKNOj7O6g
		//蜡烛图: wmJRSsWwAANl0_aEWc56wRpxvVMdUt-g
		String ExternalUserId = paramJ.getString("ExternalUserId");
		return wxWorkCommon.getExternalUserDataByHttp(ExternalUserId).toJSONString();
	}

	//员工详情
	@RequestMapping(value = {"/userDetail"})
	public String userDetail(@RequestParam Map<String, Object> params) {
		JSONObject paramJ = new JSONObject();
		params.keySet().forEach(i -> paramJ.put(i, params.get(i)));
		//YangZhiCong
		String UserId = paramJ.getString("UserId");
		return wxWorkCommon.getUserDataByHttp(UserId).toJSONString();
	}


	/** 绑定外部客户的openid */
	@RequestMapping(value = {"/bindExternalUserToWxUser"})
	public String bindExternalUserToWxUser(@RequestParam Map<String, Object> params) {
		JSONObject paramJ = new JSONObject();
		params.keySet().forEach(i -> paramJ.put(i, params.get(i)));

		return wxWorkClientService.bindExternalUserToWxUser(paramJ).toJSONString();
	}


	//客户打开优惠券事件
	@RequestMapping(value = {"/showCouponEvent"})
	public String showCouponEvent(@RequestParam Map<String, Object> params) {
		JSONObject paramJ = new JSONObject();
		params.keySet().forEach(i -> paramJ.put(i, params.get(i)));

		return wxWorkClientService.showCouponEvent(paramJ).toJSONString();
	}

}
